/*
 * JsonRpcServletContext
 * 
 * Copyright Aldrin Edison Baroi 2006
 *  
 * Aldrin Edison Baroi <aldrin91011@yahoo.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 */
package org.charless.spring.jsonrpc;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.Map;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;
import javax.servlet.descriptor.JspConfigDescriptor;

/**
 * JSON-RPC servlet is not configured using web.xml and ServletContext provided
 * by the container is immutable. JsonRpcServletContext delegates all calls to
 * ServletContext provided by the container, except "getInitParameterNames()"
 * and "getInitParameter()" calls. It delegates these call to
 * JsonRpcServletConfig.
 * 
 * 
 * @author Aldrin E Baroi
 * @author Claude CHOISNET
 * 
 */
public class JsonRpcServletContext implements ServletContext
{
	private final ServletContext servletContext;
	private final JsonRpcServletConfig jsonRpcServletConfig;

	/**
	 * 
	 */
	public JsonRpcServletContext(ServletContext servletContext)
	{
		super();

		this.servletContext       = servletContext;
		this.jsonRpcServletConfig = new JsonRpcServletConfig(this, servletContext);
	}

	public JsonRpcServletConfig getJsonRpcServletConfg()
	{
		return this.jsonRpcServletConfig;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getContext(java.lang.String)
	 */
	@Override
	public ServletContext getContext(String context)
	{
		return this.servletContext.getContext(context);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getMajorVersion()
	 */
	@Override
	public int getMajorVersion()
	{
		return this.servletContext.getMajorVersion();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getMinorVersion()
	 */
	@Override
	public int getMinorVersion()
	{
		return this.servletContext.getMinorVersion();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
	 */
	@Override
	public String getMimeType(String file)
	{
		return this.servletContext.getMimeType(file);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
	 */
	@Override
	public Set<String> getResourcePaths(String path)
	{
		 return this.servletContext.getResourcePaths(path);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getResource(java.lang.String)
	 */
	@Override
	public URL getResource(String path) throws MalformedURLException
	{
		return this.servletContext.getResource(path);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
	 */
	@Override
	public InputStream getResourceAsStream(String path)
	{
		return this.servletContext.getResourceAsStream(path);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
	 */
	@Override
	public RequestDispatcher getRequestDispatcher(String path)
	{
		return this.servletContext.getRequestDispatcher(path);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
	 */
	@Override
	public RequestDispatcher getNamedDispatcher(String name)
	{
		return this.servletContext.getNamedDispatcher(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getServlet(java.lang.String)
	 */
	@Deprecated
	@Override
	public Servlet getServlet(String name) throws ServletException
	{
		return this.servletContext.getServlet(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getServlets()
	 */
	@Deprecated
	@Override
	public Enumeration<Servlet> getServlets()
	{
		return this.servletContext.getServlets();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getServletNames()
	 */
	@Deprecated
	@Override
	public Enumeration<String> getServletNames()
	{
		return this.servletContext.getServletNames();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#log(java.lang.String)
	 */
	@Override
	public void log(String message)
	{
		this.servletContext.log(message);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#log(java.lang.Exception,
	 *      java.lang.String)
	 */
	@Deprecated
	@Override
	public void log(Exception cause, String message)
	{
		this.servletContext.log(cause, message);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#log(java.lang.String,
	 *      java.lang.Throwable)
	 */
	@Override
	public void log(String message, Throwable cause)
	{
		this.servletContext.log(message, cause);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
	 */
	@Override
	public String getRealPath(String path)
	{
		return this.servletContext.getRealPath(path);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getServerInfo()
	 */
	@Override
	public String getServerInfo()
	{
		return this.servletContext.getServerInfo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
	 */
	@Override
	public String getInitParameter(String name)
	{
		return this.jsonRpcServletConfig.getInitParameter(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getInitParameterNames()
	 */
	@Override
	public Enumeration<String> getInitParameterNames()
	{
		return this.jsonRpcServletConfig.getInitParameterNames();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
	 */
	@Override
	public Object getAttribute(String name)
	{
		return this.servletContext.getAttribute(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getAttributeNames()
	 */
	@Override
	public Enumeration<String> getAttributeNames()
	{
		return this.servletContext.getAttributeNames();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#setAttribute(java.lang.String,
	 *      java.lang.Object)
	 */
	@Override
	public void setAttribute(String name, Object value)
	{
		this.servletContext.setAttribute(name, value);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
	 */
	@Override
	public void removeAttribute(String name)
	{
		this.servletContext.removeAttribute(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContext#getServletContextName()
	 */
	@Override
	public String getServletContextName()
	{
		return this.servletContext.getServletContextName();
	}

	@Override
	public String getContextPath() 
	{
		//new CCH
		return this.servletContext.getContextPath();
	}

	@Override
	public Dynamic addFilter(String filterName, String className) 
	{
		//new CCH
		return this.servletContext.addFilter(filterName, className);
	}

	@Override
	public Dynamic addFilter(String filterName, Filter filter)
	{
		//new CCH
		return this.servletContext.addFilter(filterName, filter);
	}

	@Override
	public Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
	{
		//new CCH
		return this.servletContext.addFilter(filterName, filterClass);
	}

	@Override
	public void addListener(String className)
	{
		//new CCH
		this.servletContext.addListener(className);
	}

	@Override
	public <T extends EventListener> void addListener(T t)
	{
		//new CCH
		this.servletContext.addListener(t);
	}

	@Override
	public void addListener(Class<? extends EventListener> listenerClass)
	{
		//new CCH
		this.servletContext.addListener( listenerClass );
	}

	@Override
	public ServletRegistration.Dynamic addServlet(String servletName, String className)
	{
		//new CCH
		return this.servletContext.addServlet(servletName, className);
	}

	@Override
	public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
	{
		//new CCH
		return this.servletContext.addServlet(servletName, servlet);
	}

	@Override
	public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> className) 
	{
		//new CCH
		return this.servletContext.addServlet(servletName, className);
	}

	@Override
	public <T extends Filter> T createFilter(Class<T> clazz)
		throws ServletException 
	{
		//new CCH
		return this.servletContext.createFilter(clazz);
	}

	@Override
	public <T extends EventListener> T createListener(Class<T> clazz)
		throws ServletException
	{
		//new CCH
		return this.servletContext.createListener(clazz);
	}

	@Override
	public <T extends Servlet> T createServlet(Class<T> clazz)
		throws ServletException 
	{
		//new CCH
		return this.servletContext.createServlet(clazz);
	}

	@Override
	public void declareRoles(String... roleNames)
	{
		//new CCH
		this.servletContext.declareRoles(roleNames);
	}

	@Override
	public ClassLoader getClassLoader() 
	{
		//new CCH
		return this.servletContext.getClassLoader();
	}

	@Override
	public Set<SessionTrackingMode> getDefaultSessionTrackingModes() 
	{
		//new CCH
		return this.servletContext.getDefaultSessionTrackingModes();
	}

	@Override
	public int getEffectiveMajorVersion() 
	{
		//new CCH
		return this.servletContext.getEffectiveMajorVersion();
	}

	@Override
	public int getEffectiveMinorVersion()
	{
		//new CCH
		return this.servletContext.getEffectiveMinorVersion();
	}

	@Override
	public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() 
	{
		//new CCH
		return this.servletContext.getEffectiveSessionTrackingModes();
	}

	@Override
	public FilterRegistration getFilterRegistration(String filterName) 
	{
		//new CCH
		return this.servletContext.getFilterRegistration(filterName);
	}

	@Override
	public Map<String, ? extends FilterRegistration> getFilterRegistrations()
	{
		//new CCH
		return this.servletContext.getFilterRegistrations();
	}

	@Override
	public JspConfigDescriptor getJspConfigDescriptor() 
	{
		//new CCH
		return this.servletContext.getJspConfigDescriptor();
	}

	@Override
	public ServletRegistration getServletRegistration(String servletName)
	{
		//new CCH
		return this.servletContext.getServletRegistration(servletName);
	}

	@Override
	public Map<String, ? extends ServletRegistration> getServletRegistrations() 
	{
		//new CCH
		return this.servletContext.getServletRegistrations();
	}

	@Override
	public SessionCookieConfig getSessionCookieConfig() 
	{
		//new CCH
		return this.servletContext.getSessionCookieConfig();
	}

	@Override
	public boolean setInitParameter(String name, String value)
	{
		//new CCH
		return this.servletContext.setInitParameter(name, value);
	}

	@Override
	public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) 
	{
		//new CCH
		this.servletContext.setSessionTrackingModes(sessionTrackingModes);
	}
}
